﻿#region License
//------------------------------------------------------------
// Copyright (c) Denis Vuyka.  All rights reserved.
// This source code is subject to terms and conditions of the 
// Microsoft Reciprocal License (Ms-RL) License for Carcass. 
// A copy of the license can be found in the License.txt file 
// at the root of this distribution. 
// By using this source code in any fashion, you are agreeing 
// to be bound by the terms of the 
// Microsoft Reciprocal License (Ms-RL) License
// You must not remove this notice from this software.
//------------------------------------------------------------
#endregion

using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;

namespace Carcass.Contents
{
  [Export]
  internal sealed class DocumentViewFactory
  {
    [Import]
    private ExportProvider ExportProvider { get; set; }

    public Export<IDocumentView, IDocumentViewMetadata> CreateView(string documentType)
    {
      if (string.IsNullOrEmpty(documentType)) throw new ArgumentNullException("documentType");

      // Fetch the first occurrence of the View that satisfies the metadata provided
      var view = ExportProvider
        .GetExports<IDocumentView, IDocumentViewMetadata>(GlobalContracts.DocumentView)
        .FirstOrDefault(item => item.MetadataView.DocumentType.Contains(documentType));

      return view;
    }

    public Export<IDocumentView, IDocumentViewMetadata> CreateView(IDocumentMetadata metadata)
    {
      return CreateView(metadata.DocumentType);
    }
  }
}